﻿using System;
using System.Data;
using System.Data.Common;

namespace DBA.Data.ADO
{
    /// <summary>
    /// 访问SQL Server 的help
    /// </summary>
    class SqlClientDAL : DataAccessLibrary
    {
        /// <summary>
        /// 子类的构造函数，调用父类的构造函数
        /// </summary>
        public SqlClientDAL()
        {
        }

        public SqlClientDAL(DBA.Data.Hibernate.NHibernateQuery nhQuery)
        {

        }

        /// <summary>
        /// 子类的构造函数，调用父类的构造函数
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        /// <param name="providerName">数据库驱动类型的名称。System.Data.SqlClient、System.Data.OleDb、System.Data.Odbc、System.Data.OracleClient 等</param>
        public SqlClientDAL(string connectionString, string  providerName)
            : base(connectionString, providerName)
        {
        }

        /// <summary>
        /// 设置存储过程的参数名称的前缀。MS Sql返回“@”
        /// </summary>
        /// <returns></returns>
        public override string ParameterPrefix()
        {
            return "@";
        }

        #region ExecuteExists
        /// <summary>
        /// 执行一条SQL语句，看是否能查到记录 有：返回true；没有返回false，用于判断是否重名
        /// </summary>
        /// <param name="sql">查询语句。比如select ID from tableName where userName='aa'</param>
        /// <returns></returns>
        public virtual bool ExecuteExists(string sql)
        {
            string tmpSQL = " select 1 where (exists({0}))";

            SetCommand(string.Format(tmpSQL, sql));		//设置command

            DbDataReader r = null;
            try
            {
                this.ConnectionOpen();

                r = Command.ExecuteReader();
                bool re = true;
                if (r.HasRows)
                    re = true;
                else
                    re = false;

                return re;
            }
            catch (Exception ex)
            {
                SetErrorMessage("ExecuteExists", sql, ex.Message);	//处理错误
                return true;
            }
            finally
            {
                if (r != null) r.Close();

                if (!IsUseTrans)        //判断是否使用了事务，没有使用事务的情况下，才可以关闭连接
                    Command.Connection.Close();
            }
        }
        #endregion


        #region ExecuteStore
        public override int ExecuteStore(string sql)
        {
            return ExecuteStore(sql, "ExecuteStore");
        }
        public override int ExecuteStore(string sql, string functionName)
        {
            SetStoredCommand(sql);		//设置command
            int reValue = 0;
            try
            {
                this.ConnectionOpen();
                _ExecuteRowCount = Command.ExecuteNonQuery();
                reValue = 1;

            }
            catch (Exception ex)
            {
                SetErrorMessage(functionName, sql, ex.Message);	//处理错误
            }
            finally
            {
                if (!IsUseTrans)            //判断是否使用了事务，没有使用事务的情况下，才可以关闭连接
                    Command.Connection.Close();
            }
            return reValue;
        }
        #endregion
    }
}
